package com.wmada.toolkit.rest;

import org.apache.http.client.methods.HttpRequestBase;

import java.util.HashMap;
import java.util.Map;

/**
 * @author will.ma
 * @date 2019-4-25
 */
public class DownloadException extends Exception {

    public static final int STATUS_REQUEST_ERROR        = 600;
    public static final int STATUS_WRONG_URL_FORMAT     = 601;
    public static final int STATUS_UNSUPPORTED_ENCODING = 602;

    private static final Map<Integer, String> MSG = new HashMap<>();

    static {
        MSG.put(STATUS_REQUEST_ERROR, "Request Error.");
        MSG.put(STATUS_WRONG_URL_FORMAT, "Wrong URL Format.");
        MSG.put(STATUS_UNSUPPORTED_ENCODING, "Unsupported Encoding.");
    }

    private Response response;

    public DownloadException(HttpRequestBase request, int status) {
        super(MSG.getOrDefault(status, "Unknown Error."));
        this.response = new Response(request, status, MSG.getOrDefault(status, "Unknown Error."));
    }

    public DownloadException(Throwable cause, HttpRequestBase request, int status) {
        super(MSG.getOrDefault(status, "Unknown Error."), cause);
        this.response = new Response(request, status, MSG.getOrDefault(status, "Unknown Error."));
    }

    public Response getResponse() {
        return response;
    }

    public void setResponse(Response response) {
        this.response = response;
    }
}
